//
//  CXYHomeViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYHomeViewController: CXYBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        setupUI()
    }
    
    override func setupUI() {
        self.title = "主页"
        let homeBannerView = UINib.init(nibName: "CXYHomeBannerView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! CXYHomeBannerView
        self.tableView.tableHeaderView = homeBannerView
        
        self.tableView.register(UINib.init(nibName: "CXYHomeNewsCell", bundle: nil), forCellReuseIdentifier: "CXYHomeNewsCell")
        self.tableView.register(UINib.init(nibName: "CXYHomeActiveItemCell", bundle: nil), forCellReuseIdentifier: "CXYHomeActiveItemCell")
        self.tableView.tableFooterView = UIView()
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = indexPath.section
        if section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CXYHomeNewsCell", for: indexPath)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CXYHomeActiveItemCell", for: indexPath)
            return cell
        }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
           return 3
        } else {
            return 1
        }
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            let activeSesstionView = UINib.init(nibName: "CXYHomeActiveSessionView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! CXYHomeActiveSessionView
            return activeSesstionView
        } else {
            let newsSesstionView = UINib.init(nibName: "CXYHomeNewsSessionView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! CXYHomeNewsSessionView
            return newsSesstionView
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10.0
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let section = indexPath.section
        if section == 0 {
            return 80.0
        } else {
            return 165.0
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        self.performSegue(withIdentifier: "loadnext", sender: nil)
    }
    
    
}
